//
//  NetworkTools.swift
//  Home_school_晓林
//
//  Created by 张晓林 on 16/9/25.
//  Copyright © 2016年 张晓林. All rights reserved.
//

import UIKit
import AFNetworking

// 定义一个闭包类型
typealias callBack = (_ data: Any?, _ error: Error?) -> ()

class NetworkTools: AFHTTPSessionManager {
    
    /// swift的单例
    static let shared: NetworkTools = {
        let tool = NetworkTools(baseURL: URL(string: basURL))
        
        tool.responseSerializer.acceptableContentTypes?.insert("text/html")
        
        return tool
    }()
    
    
    /// 网络方法
    ///
    /// - parameter urlStr:     接口字符串
    /// - parameter parameters: 参数字典
    /// - parameter finished:   完成回调
    func requestWithGet(urlStr: String,
                        parameters: Any?,
                        finished: @escaping callBack) {
        // AFN的方法
        get(urlStr, parameters: parameters, progress: nil, success: { (_, data) in
                finished(data, nil)
            }) { (_, error) in
               finished(nil, error)
        }
    }

}
